package com.zyd.shiro.controller;

import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.enums.ResponseStatus;
import com.zyd.shiro.business.service.MajorService;
import com.zyd.shiro.business.vo.MajorConditionVO;
import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.persistence.beans.Major;
import com.zyd.shiro.util.ResultUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/major")
public class RestMajorController {

    @Autowired
    private MajorService majorService;

    @RequiresPermissions("major")
    @PostMapping("/list")
    public PageResult getAll(MajorConditionVO vo) {
        PageInfo<Major> pageInfo = majorService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    @RequiresPermissions("major:add")
    @PostMapping(value = "/add")
    public ResponseVO add(Major major) {
        try {
            majorService.insertMajor(major);
            return ResultUtil.success("成功");
        }catch (Exception e){
            return ResultUtil.error("添加失败:" + e.getMessage());
        }
    }

    @RequiresPermissions(value = {"major:batchDelete", "major:delete"}, logical = Logical.OR)
    @PostMapping(value = "/remove")
    public ResponseVO remove(Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请至少选择一条记录");
        }
        for (Long id : ids) {
            majorService.removeByPrimaryKey(id);
        }
        return ResultUtil.success("成功删除 [" + ids.length + "] 个专业");
    }

    @RequiresPermissions("major:edit")
    @PostMapping("/get/{id}")
    public ResponseVO get(@PathVariable Long id) {
        return ResultUtil.success(null, this.majorService.getByPrimaryKey(id));
    }

    @RequiresPermissions("major:edit")
    @PostMapping("/edit")
    public ResponseVO edit(Major major) {
        try {
            majorService.updateMajorSelective(major);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("专业修改失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }
}
